package com.tengju.support.application.article;

import com.tengju.support.application.shared.ApplicationException;
import com.tengju.support.application.shared.ApplicationExceptionCode;
import com.tengju.support.domain.model.article.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Service
public class ArticleService {

    @Autowired
    private ArticleRepository articleRepository;

    @Transactional
    public ArticleId add(String title, ArticleType type, String context, String operator) {
        Article article = ArticleFactory.create(title, type, context, operator);
        return articleRepository.insert(article);
    }

    public int edit(ArticleId articleId, String title, ArticleType type, String context, String operator) {

        Article article = articleRepository.get(articleId);
        if (article == null) {
            throw ApplicationException.error(ApplicationExceptionCode.ARTICLE_NOT_EXIST,articleId.getValue());
        }
        article.edit(title,type,context,operator);

        return articleRepository.edit(article);
    }

    public int del(ArticleId articleId) {
        return articleRepository.del(articleId);
    }

    public int updateArticleViewCnt(ArticleId articleId) {
        return articleRepository.updateViewCnt(articleId);
    }
}
